package net.cooltain.warhammer.running.event;

import java.util.Map;

import net.cooltain.warhammer.framework.constant.ConstantPlayZone;
import net.cooltain.warhammer.running.entity.MotionCard;
import net.cooltain.warhammer.running.entity.MotionPlayerZone;
import net.cooltain.warhammer.running.entity.ZoneEntity;

/**
 * 卡片位置转移的事件
 * @author cooltian
 *
 */
public class MoveCardEvent {
	
	static int STATUS_NORMAL = 0; // 开始
	static int STATUS_CANNEL  = -1; // 取消,返回原位置
	static int STATUS_FAIL    = -2; // 失败了,放到failToZone指定的位置
	
	MotionCard card ;
	int eventStatus;     // 事件的状态
	MotionPlayerZone sourceFromPlayer ; // 原来的玩家
	MotionPlayerZone successToPlayer ;  // 成功后到达玩家
	int sourceFromZone ; // 原来的区域
	int successToZone ;  // 成功后到达的区域:需要指定
	
	int cannelToZone ;   // 被取消后到达的区域:默认原位置
	int failToZone ;     // 失败后到达的区域:默认弃牌堆
	
	
	/**
	 * 将卡片转移到目标区域
	 * @param card
	 * @param successToZone
	 */
	public static boolean moveACard(MotionCard card , int successToZone){
		return moveACard(card, card.cardEntity.getPlayer(), successToZone);
	}
	
	/**
	 * 将卡片转移指定玩家指定区域
	 * @param player1
	 * @param player2
	 * @param card
	 * @param successToZone
	 */
	public static boolean moveACard(MotionCard card , MotionPlayerZone tarPlayer,int successToZone){
		return moveACard(card, tarPlayer, successToZone, card.currentZone);
	}
	
	/**
	 * 将卡片转移指定玩家指定区域
	 * @param tarPlayer
	 * @param card
	 * @param successToZone
	 * @param cannelToZone
	 * @return
	 */
	public static boolean moveACard(MotionCard card , MotionPlayerZone tarPlayer,int successToZone, int failToZone){
		MoveCardEvent event = new MoveCardEvent();
		event.card = card;
		event.eventStatus = STATUS_NORMAL;
		event.sourceFromPlayer = card.cardEntity.getPlayer();
		event.sourceFromZone = card.currentZone;
		event.successToPlayer = tarPlayer;
		event.successToZone = successToZone ;
		event.cannelToZone = card.currentZone;
		event.failToZone = ConstantPlayZone.DISCARD_ZONE;
		
		// TODO : 
		// 抛出事件，判断这次事件是否成功
		
		// 处理返回后的事件
		if(event.eventStatus==STATUS_CANNEL){
			System.out.println("被取消了");
			return false;
		}else if(event.eventStatus == STATUS_FAIL){
			System.out.println("失败了");
			return false;
		}else{
			Map<Integer, ZoneEntity> srcZoneMap = card.cardEntity.getPlayer().allZoneMap;
			Map<Integer, ZoneEntity> targetZoneMap = tarPlayer.allZoneMap;
			ZoneEntity srcZone = srcZoneMap.get(card.currentZone);
			ZoneEntity tarZone = targetZoneMap.get(successToZone);
			srcZone.eventRemoveCard(card);
			tarZone.eventAddCard(card);
			return true;
		}
		
	}
}
